package com.syf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syf.dto.UserDto;
import com.syf.feign.UserServiceFeign;
import com.syf.domain.WorkIssue;
import com.syf.mapper.WorkIssueMapper;
import com.syf.service.WorkIssueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

/**
*
*
*@author 苏耀奋
*@create 2021-07-11 12:11
*/
@Service
public class WorkIssueServiceImpl extends ServiceImpl<WorkIssueMapper, WorkIssue> implements WorkIssueService {

    @Autowired
    private UserServiceFeign userServiceFeign;

    /**
     * 条件分页查询客服工单列表【客服与会员的聊天】
     *
     * @param page      分页参数
     * @param status    工单的状态
     * @param startTime 查询的工单创建起始时间
     * @param endTime   查询的工单创建截至时间
     * @return
     */
    @Override
    public Page<WorkIssue> findByPage(Page<WorkIssue> page, Integer status, String startTime, String endTime) {
        //1.条件分页 查询客服工单信息
        Page<WorkIssue> pageData = page(page, new LambdaQueryWrapper<WorkIssue>()
                .eq(status != null, WorkIssue::getStatus, status)
                .between(
                        !StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime),
                        WorkIssue::getCreated,  //表字段
                        startTime, endTime + " 23:59:59")
        );
        List<WorkIssue> records = pageData.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return pageData;  //没有信息，则返回
        }


       /* for (WorkIssue record : records) {   错误的示范
            Long userId = record.getUserId();
            // 使用userId->远程调用member-service
        }*/
        //2 收集 会员用户Id
        List<Long> userIds = records.stream().map(WorkIssue::getUserId).collect(Collectors.toList());

        //3 远程调用member-service 查询会员信息: list<UserDto> -> map<id,userDto> 这操作已经在member模块那边查询时做了
        /*List<UserDto> basicUsers = userServiceFeign.getBasicUsers(userIds);

        if(CollectionUtils.isEmpty(basicUsers)){
            return pageData ;
        }
        2.1 小技巧: list<UserDto> -> map<id,userDto>
        Map<Long, UserDto> idMapUserDtos = basicUsers.stream().
                                collect(
                                            Collectors.toMap(
                                                    userDto -> userDto.getId(),  // key
                                                    userDto -> userDto) //value
                                );*/

        Map<Long, UserDto> idMapUserDtos = userServiceFeign.getBasicUsers(userIds, null, null);

        //4 循环每一个workIssue ,给它里面设置用户的信息 map.get(userId)
        records.forEach(workIssue -> {
            UserDto userDto = idMapUserDtos.get(workIssue.getUserId());
            workIssue.setUsername(userDto == null ? "测试用户" : userDto.getUsername());
            workIssue.setRealName(userDto == null ? "测试用户" : userDto.getRealName());
        });


        return pageData;
    }


    /**
     * 前台: 查询会员自己 的客服工单
     *
     * @param page
     * @return
     */
    @Override
    public Page<WorkIssue> getIssueList(Page<WorkIssue> page, Long userId) {
        return page(page,new LambdaQueryWrapper<WorkIssue>()
                        .eq(WorkIssue::getUserId,userId)
        );
    }
}
